package com.cyj.qqserver.service;

import com.cyj.qqcommon.Message;
import com.cyj.qqcommon.MessageType;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

public class ServerConnectClientThread extends Thread implements Serializable {
    private Socket socket;
    private String userId;
    public ServerConnectClientThread(Socket socket,String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    @Override
    public void run() {
        //发送接收消息
        while (true) {
            System.out.println("服务端和客户端"+userId+"保持通信，读取数据中。。。。");
            try {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message=(Message)ois.readObject();
                if(message.getMessageType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)){
                    System.out.println(message.getSender()+"申请查看在线用户");
                    String onlineUsers=ManageServerConnectClientThread.getOnlineUsers();
                    Message message2 = new Message();
                    message2.setMessageType(MessageType.MESSAGE_RET_OFFLINE_FRIEND);
                    message2.setContent(onlineUsers);
                    message2.setGetter(message.getSender());//写入socket
                    ObjectOutputStream oos= new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message2);
                }else if(message.getMessageType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                    System.out.println(message.getSender()+"将退出");
                    ManageServerConnectClientThread.removeClient(message.getSender());
                    socket.close();
                    break;//退出while
                } else if (message.getMessageType().equals(MessageType.MESSAGE_COMM_MES)) {
                    //根据message获取getterid，再提取线程
                    ServerConnectClientThread serverConnectClientThread =
                            ManageServerConnectClientThread.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.socket.getOutputStream());
                    oos.writeObject(message);
                } else if (message.getMessageType().equals(MessageType.MESSAGE_TOALL_MES)) {
                    HashMap<String,ServerConnectClientThread> hm=new ManageServerConnectClientThread().getHm();
                    Iterator<String> iterator=hm.keySet().iterator();
                    while(iterator.hasNext()){
                        String OnlineUserId=iterator.next().toString();
                        if(!(message.getSender().equals(OnlineUserId))){
                            ObjectOutputStream oos = new ObjectOutputStream(hm.get(OnlineUserId).socket.getOutputStream());
                            oos.writeObject(message);
                        }
                    }
                } else if (message.getMessageType().equals(MessageType.MESSAGE_FILE_MES)) {
                    ServerConnectClientThread serverConnectClientThread =
                            ManageServerConnectClientThread.getServerConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(serverConnectClientThread.socket.getOutputStream());
                    oos.writeObject(message);
                }
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
